package org.railage.server.services;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.log4j.Logger;
import org.railage.common.dto.TicketOfUserDTO;
import org.railage.common.protocol.Result;
import org.railage.server.dao.TicketDAO;
import org.railage.server.dao.TicketDAOImpl;
import org.railage.server.dao.TicketOfUserDAO;
import org.railage.server.dao.TicketOfUserDAOImpl;
import org.railage.server.dao.UserDAO;
import org.railage.server.dao.UserDAOImpl;
import org.railage.server.entities.TicketOfUser;
import org.railage.server.entities.User;
import org.railage.server.util.HibernateUtil;
import org.railage.server.util.Utils;

public class TicketService {

	private static final Logger log = Logger.getLogger(TicketService.class);
	
	private TicketDAO ticketDAO = new TicketDAOImpl();
	private TicketOfUserDAO ticketOfUserDAO = new TicketOfUserDAOImpl();
	private UserDAO userDAO = new UserDAOImpl();

	public Result buyTicket(int userID, int tripID, String stationA,
			String stationB) {
		int resultCode = ticketDAO.sellTicket(userID, tripID, stationA, stationB);
		
		Result result = null;
		switch (resultCode) {
		case 0:
			result = new Result(true, "Ticket successfully bought!");
			break;
		case 1:
			result = new Result(false, "No available seats!");
			break;
		case 2:
		case 3:
		case 6:
		case 7:
			result = new Result(false, "You are already registered on this trip!");
			break;
		case 4:
		case 5:
			result = new Result(false, "There is less than 10 minutes before start!");
			break;
		case -1:
			result = new Result(false, HibernateUtil.DB_SESSION_ERROR_MSG);
			break;
		default:
			break;
		}
		return result;
	}

	public Result buyTicket(Object obj) {
		Object[] params = (Object[]) obj;
		return buyTicket((Integer) params[0], (Integer) params[1],
				(String) params[2], (String) params[3]);
	}

	public Result sellTicket(Object obj) {
		Object[] params = (Object[]) obj;

		String surname = (String) params[0];
		String name = (String) params[1];
		String birthday = (String) params[2];
		int tripID = (Integer) params[3];
		String stationA = (String) params[4];
		String stationB = (String) params[5];

		int userID = -1;
		User user = userDAO.getUserByPersonalInfo(surname, name, birthday);
		if (user != null) {
			userID = user.getId();
		} else {
			try {
				user = new User();
				user.setRole(0);
				user.setLogin(surname.toLowerCase());
				user.setPassword(Utils.passwordGen(8));
				user.setSurname(surname);
				user.setName(name);
				user.setBirthday(new SimpleDateFormat("yyyy-MM-dd")
						.parse(birthday));
				userID = userDAO.addUser(user);
			} catch (Exception e) {
				log.error("Exception occured: ", e);
			}
		}
		if (userID != -1) {
			return buyTicket(userID, tripID, stationA, stationB);
		} else {
			return new Result(false, HibernateUtil.DB_SESSION_ERROR_MSG);
		}
	}

	public TicketOfUserDTO[] getAllTicketsOfUser(Object obj) {
		List<TicketOfUser> tickets = ticketOfUserDAO
				.getAllTicketsOfUser((Integer) obj);
		TicketOfUserDTO[] ticketsDTO = new TicketOfUserDTO[tickets.size()];
		int i = 0;
		for (TicketOfUser ticket : tickets) {
			ticketsDTO[i] = ticket.toDTO();
			i++;
		}
		return ticketsDTO;
	}

	public int getAvailableTicketsNumber(Object obj) {
		String[] params = (String[]) obj;
		return ticketDAO.getAvailableTicketsNumber(Integer.parseInt(params[0]),
				params[1], params[2]);
	}

}
